﻿using System.Web.Mvc;
using SeaBattle.UI.Web.Models;
using SeaBattle.Domain.Abstract.Services;
using SeaBattle.UI.Web.Models.ChildActions;
using System.Linq;

namespace SeaBattle.UI.Web.Controllers
{
    [Authorize]
    public class RoomController : Controller
    {
        private IPlayerService playerService;
        private IAccountService accountService;
        private IGameService gameService;

        public RoomController(IPlayerService playerService, 
            IAccountService accountService, 
            IGameService gameService)
        {
            this.playerService = playerService;
            this.accountService = accountService;
            this.gameService = gameService;
        }
        //
        // GET: /Entrance/
        public ActionResult Index()
        {
            // Get player's account instance
            var player = playerService.GetPlayer(User.Identity.Name);
            // Ask the account for the balance value
            var balance = accountService.GetPlayerBalance(player);

            // Get game list
            var gameList = gameService.GetOpenedGames().Where(x=>x.Creator!=player);
            return View(new RoomViewModel
                            {
                                PlayerAccountBalance = balance,
                                GameList = gameList
                            });                
        }

        [ChildActionOnly]
        public ActionResult GameList(string playerLogin)
        {
            // Get player instance
            var player = playerService.GetPlayer(playerLogin);
            // Get all player's games
            var gameList = gameService.GetParticipantsPlayerGames(player)
                .Where(x=>x.Opponent!=null);
            // 
            return View("ChildActions/GameList", new GameListViewModel { Games = gameList, UserLogin = User.Identity.Name });

        }

    }
}
